package com.lineage.data.item_etcitem.add;

import com.lineage.data.event.PowerItemSet;
import com.lineage.data.executor.ItemExecutor;
import com.lineage.server.datatables.lock.CharItemPowerReading;
import com.lineage.server.model.Instance.L1ItemInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.serverpackets.S_ItemName;
import com.lineage.server.serverpackets.S_ItemStatus;
import com.lineage.server.serverpackets.S_ServerMessage;
import com.lineage.server.serverpackets.ServerBasePacket;
import com.lineage.server.templates.L1ItemPower_name;
import com.lineage.server.utils.log.PlayerLogUtil;
import java.sql.Timestamp;
import java.util.Random;

public class hole_armor extends ItemExecutor {
  private int _type;
  
  private int a;
  
  private static final Random _random = new Random();
  
  public static ItemExecutor get() {
    return new hole_armor();
  }
  
  public void execute(int[] data, L1PcInstance pc, L1ItemInstance item) {
    int targObjId = data[0];
    L1ItemInstance tgItem = pc.getInventory().getItem(targObjId);
    if (tgItem == null)
      return; 
    if (tgItem.isEquipped()) {
      pc.sendPackets((ServerBasePacket)new S_ServerMessage("\\fR你必须先解除物品装备!"));
      return;
    } 
    L1ItemPower_name power = null;
    boolean update = false;
    int count = 0;
    switch (tgItem.getItem().getUseType()) {
      case 2:
      case 13:
      case 18:
      case 19:
      case 20:
      case 21:
      case 22:
      case 25:
        count = PowerItemSet.ARMORHOLE;
        if (tgItem.get_power_name() != null) {
          power = tgItem.get_power_name();
          update = true;
          break;
        } 
        power = new L1ItemPower_name();
        break;
    } 
    if (tgItem.getItem().getUseType() == 2 || tgItem.getItem().getUseType() == 18 || 
      tgItem.getItem().getUseType() == 19 || tgItem.getItem().getUseType() == 20 || 
      tgItem.getItem().getUseType() == 21 || tgItem.getItem().getUseType() == 22 || 
      tgItem.getItem().getUseType() == 13 || tgItem.getItem().getUseType() == 25) {
      if (power.get_hole_count() == PowerItemSet.ARMORHOLE) {
        pc.sendPackets((ServerBasePacket)new S_ServerMessage("\\fT扩充数量已经满了"));
        return;
      } 
      switch (this._type) {
        case 1:
          if (power == null)
            break; 
          if (_random.nextInt(100) + 1 <= this.a) {
            power.set_item_obj_id(tgItem.getId());
            power.set_hole_count(power.get_hole_count() + 1);
            打洞成功("玩家 :" + pc.getName() + "使用 " + item.getName() + "(成功打出1洞) ,时间:" + 
                new Timestamp(System.currentTimeMillis()) + ")");
            tgItem.set_power_name(power);
            pc.sendPackets((ServerBasePacket)new S_ServerMessage("\\fR成功增加了洞数-最高可达" + PowerItemSet.ARMORHOLE + "个洞(请注意)"));
            if (update) {
              CharItemPowerReading.get().updateItem(tgItem.getId(), tgItem.get_power_name());
              pc.sendPackets((ServerBasePacket)new S_ItemStatus(tgItem));
              pc.sendPackets((ServerBasePacket)new S_ItemName(tgItem));
            } else {
              pc.sendPackets((ServerBasePacket)new S_ItemStatus(tgItem));
              pc.sendPackets((ServerBasePacket)new S_ItemName(tgItem));
              CharItemPowerReading.get().storeItem(tgItem.getId(), tgItem.get_power_name());
            } 
            pc.sendPackets((ServerBasePacket)new S_ItemName(tgItem));
            break;
          } 
          pc.sendPackets((ServerBasePacket)new S_ServerMessage("\\fR强化失败..运气不好"));
          break;
        case 2:
          if (power == null)
            break; 
          if (_random.nextInt(100) + 1 <= this.a) {
            power.set_item_obj_id(tgItem.getId());
            power.set_hole_count(power.get_hole_count() + 1);
            tgItem.set_power_name(power);
            pc.sendPackets((ServerBasePacket)new S_ServerMessage("\\fR成功增加了洞数-最高可达" + PowerItemSet.ARMORHOLE + "个洞(请注意)"));
            打洞成功("玩家 :" + pc.getName() + "使用 " + item.getName() + "(成功打出1洞) ,时间:" + 
                new Timestamp(System.currentTimeMillis()) + ")");
            if (update) {
              CharItemPowerReading.get().updateItem(tgItem.getId(), tgItem.get_power_name());
              pc.sendPackets((ServerBasePacket)new S_ItemStatus(tgItem));
              pc.sendPackets((ServerBasePacket)new S_ItemName(tgItem));
            } else {
              pc.sendPackets((ServerBasePacket)new S_ItemStatus(tgItem));
              pc.sendPackets((ServerBasePacket)new S_ItemName(tgItem));
              CharItemPowerReading.get().storeItem(tgItem.getId(), tgItem.get_power_name());
            } 
            pc.sendPackets((ServerBasePacket)new S_ItemName(tgItem));
            break;
          } 
          pc.sendPackets((ServerBasePacket)new S_ServerMessage("\\fR强化失败..运气不好"));
          break;
        case 3:
          if (power == null)
            break; 
          if (_random.nextInt(100) + 1 <= this.a) {
            power.set_item_obj_id(tgItem.getId());
            power.set_hole_count(power.get_hole_count() + 1);
            tgItem.set_power_name(power);
            pc.sendPackets((ServerBasePacket)new S_ServerMessage("\\fR成功增加了洞数-最高可达" + PowerItemSet.ARMORHOLE + "个洞(请注意)"));
            打洞成功("玩家 :" + pc.getName() + "使用 " + item.getName() + "(成功打出1洞) ,时间:" + 
                new Timestamp(System.currentTimeMillis()) + ")");
            if (update) {
              CharItemPowerReading.get().updateItem(tgItem.getId(), tgItem.get_power_name());
              pc.sendPackets((ServerBasePacket)new S_ItemStatus(tgItem));
              pc.sendPackets((ServerBasePacket)new S_ItemName(tgItem));
            } else {
              pc.sendPackets((ServerBasePacket)new S_ItemStatus(tgItem));
              pc.sendPackets((ServerBasePacket)new S_ItemName(tgItem));
              CharItemPowerReading.get().storeItem(tgItem.getId(), tgItem.get_power_name());
            } 
            pc.sendPackets((ServerBasePacket)new S_ItemName(tgItem));
            break;
          } 
          pc.sendPackets((ServerBasePacket)new S_ServerMessage("\\fR强化失败..运气不好"));
          break;
        case 4:
          if (power == null)
            break; 
          if (_random.nextInt(100) + 1 <= this.a) {
            power.set_item_obj_id(tgItem.getId());
            power.set_hole_count(power.get_hole_count() + 1);
            tgItem.set_power_name(power);
            pc.sendPackets((ServerBasePacket)new S_ServerMessage("\\fR成功增加了洞数-最高可达" + PowerItemSet.ARMORHOLE + "个洞(请注意)"));
            打洞成功("玩家 :" + pc.getName() + "使用 " + item.getName() + "(成功打出1洞) ,时间:" + 
                new Timestamp(System.currentTimeMillis()) + ")");
            if (update) {
              CharItemPowerReading.get().updateItem(tgItem.getId(), tgItem.get_power_name());
              pc.sendPackets((ServerBasePacket)new S_ItemStatus(tgItem));
              pc.sendPackets((ServerBasePacket)new S_ItemName(tgItem));
            } else {
              pc.sendPackets((ServerBasePacket)new S_ItemStatus(tgItem));
              pc.sendPackets((ServerBasePacket)new S_ItemName(tgItem));
              CharItemPowerReading.get().storeItem(tgItem.getId(), tgItem.get_power_name());
            } 
            pc.sendPackets((ServerBasePacket)new S_ItemName(tgItem));
            break;
          } 
          pc.sendPackets((ServerBasePacket)new S_ServerMessage("\\fR强化失败..运气不好"));
          break;
        case 5:
          if (power == null)
            break; 
          if (_random.nextInt(100) + 1 <= this.a) {
            power.set_item_obj_id(tgItem.getId());
            power.set_hole_count(power.get_hole_count() + 1);
            tgItem.set_power_name(power);
            pc.sendPackets((ServerBasePacket)new S_ServerMessage("\\fR成功增加了洞数-最高可达" + PowerItemSet.ARMORHOLE + "个洞(请注意)"));
            打洞成功("玩家 :" + pc.getName() + "使用 " + item.getName() + "(成功打出1洞) ,时间:" + 
                new Timestamp(System.currentTimeMillis()) + ")");
            if (update) {
              CharItemPowerReading.get().updateItem(tgItem.getId(), tgItem.get_power_name());
              pc.sendPackets((ServerBasePacket)new S_ItemStatus(tgItem));
              pc.sendPackets((ServerBasePacket)new S_ItemName(tgItem));
            } else {
              pc.sendPackets((ServerBasePacket)new S_ItemStatus(tgItem));
              pc.sendPackets((ServerBasePacket)new S_ItemName(tgItem));
              CharItemPowerReading.get().storeItem(tgItem.getId(), tgItem.get_power_name());
            } 
            pc.sendPackets((ServerBasePacket)new S_ItemName(tgItem));
            break;
          } 
          pc.sendPackets((ServerBasePacket)new S_ServerMessage("\\fR强化失败..运气不好"));
          break;
      } 
      pc.getInventory().removeItem(item, 1L);
    } else {
      pc.sendPackets((ServerBasePacket)new S_ServerMessage("\\fR该物品无法扩充"));
    } 
  }
  
  public void set_set(String[] set) {
    try {
      this._type = Integer.parseInt(set[1]);
    } catch (Exception exception) {}
    try {
      this.a = Integer.parseInt(set[2]);
    } catch (Exception exception) {}
  }
  
  public static void 打洞成功(String info) {
    PlayerLogUtil.writeLog("打洞成功", info);
  }
}
